const app = getApp()
var api = require('../../../utils/api.js')
var util = require('../../../utils/util.js');
Page({
  data: {
    scrollIndex:0,
    listCol:true
  },
  onLoad:function(){
    
  },
  onShow: function() {
    this.getFriends();
    var nickname = wx.getStorageSync('accountInfo').nickname;
    this.setData({
      nickname: nickname,
      scrollIndex:0,
      scrollTop:0
    })
  },

//获取好友列表
getFriends:function(){

  var that = this;
  api.POST({
    params: {},
    url: app.globalData.base_url + 'askWine/friendsList',
    success: function (res) {
      if (util.isEmpty(res.data.code)) {
        that.setData({
          userlist: res.data.data.list
        })
        if(res.data.data.list.length>0){
          that.sortingList();
          that.setData({
            count: res.data.data.count
          })
        }else{
          that.setData({
            list: []
          })
        }
        
      }

    },
    fail: function (fail) {
      util.showError("获取失败，请检查网络");
    }
  })

},


  //好友列表序列化处理
  sortingList: function() {
    const list = this.data.userlist;
    // console.log(list);
    const map = {}
    list.forEach((item, index) => {
      let key = item.first_charter
      if (!map[key]) {
        map[key] = {
          name: key,
          list: []
        }
      }
      map[key].list.push(item)
    })

    let hot = []
    let ret = []
    for (let key in map) {
      let val = map[key]
      if (/[a-zA-Z]/.test(val.name)) {
        ret.push(val)
      } else if (val.name === "#") {
        hot.push(val)
      }
    }
    ret.sort(function(a, b) {
      return a.name.charCodeAt(0) - b.name.charCodeAt(0)
    })

    const userList = ret.concat(hot)
    this.setData({
      list: userList
    })
    this.getBoxHeight();
  },


  //获取盒子高度
  getBoxHeight:function(){
    var that = this
    const query = wx.createSelectorQuery()
    query.selectAll('.box').boundingClientRect()
    query.exec(function (res) {
      // console.log(res);
      const list = res[0];
      const box =[]
      for(let i=0;i<list.length;i++){
        box.push(Math.floor(list[i].top-40));
      }
      that.setData({
        heightBox: box
      })
    })
    
  },

  //滑动列表
  scrollMove:function(e){
    let scrollTop = e.detail.scrollTop;
    const heightBox = this.data.heightBox

    if (scrollTop > heightBox[heightBox.length-1]){
      var index = (heightBox.length - 1).toString()

      this.setData({
        scrollIndex: index
      })
      return
    }

    heightBox.forEach((item, index) => {
      // console.log(item)
      if (scrollTop > item && scrollTop < heightBox[index / 1 + 1]) {
        var index = index.toString()

        this.setData({
          scrollIndex: index
        })
      }
    })

  },

  //添加好友
  addFriend:function(){

    wx.navigateTo({
      url: '/play/pages/addFriend/addFriend',
    })

  },

  //右侧筛选
  scrollIdSel:function(e){
    // console.log(e);
    var index = e.currentTarget.dataset.index;
    var scrollTop=this.data.heightBox[index]
    this.setData({
      scrollIndex:index,
      scrollTop: scrollTop
    })
  },
  friendWine:function(e){
    var uid = e.currentTarget.dataset.uid;
    var nickname = e.currentTarget.dataset.nickname;

    wx.navigateTo({
      url: '/play/pages/friendWine/friendWine?uid=' + uid + '&userName=' + nickname,
    })

  },

  onShareAppMessage: function (res) {
    var that = this;

    if (res.from === 'button') {
      // 来自页面内转发按钮
      return {
        title: this.data.nickname +'向您分享了飘香酒掌柜小程序',
        path: '/pages/index/index',
        imageUrl: '/images/share.jpg',
        success: function (res) {
          // 转发成功
        },
        fail: function (res) {
          // 转发失败
        }
      }
    }
  },
  deleteSearch:function(){
    this.setData({
      searchText:'',
      listCol:true
    })
  },
  searchInput:function(e){
    var keyWord = e.detail.value; 
    // 假设现在需要检测到用户输入的值，用户 400 毫秒内没有继续输入就将该值打印出来
    this.throttle(this.getSearchList, null, 400, keyWord);
  },

// 节流
  throttle: function (fn, context, delay, text) {
    clearTimeout(fn.timeoutId);
    fn.timeoutId = setTimeout(function () {
      fn.call(context, text);
    }, delay);
  },
  // 想要执行的函数
  getSearchList: function (text) {
    var that = this;
    if (text){
      util.showLoading("加载中...");
      var params = new Object()
      var that = this;
      params.nikename = text;

      api.POST({
        params: params,
        url: app.globalData.base_url + 'askWine/searchFriends',
        success: function (res) {
          wx.hideLoading();
          if (util.isEmpty(res.data.code)) {
            if (res.data.data.length>0){
              var searchList = res.data.data
            }else{
              var searchList =[]
            }
            that.setData({
              listCol:false,
              searchList: searchList
            })
          }
        },
        fail: function (fail) {
          util.showError("网络错误，请检查网络");
        }
      })

    }else{
      this.setData({
        listCol:true
      })
    }
  },

  onShareAppMessage: function (res) {
    var that = this;
    if (res.from === 'button') {
      // 来自页面内转发按钮
      return {
        title: '飘香酒掌柜',
        path: '/pages/index/index',
        imageUrl: '/images/share.jpg',
        complete() {
        }
      }
    }
  },


})